package com.dongge;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by Administrator on 2016/7/3.
 */
public class SpringShiro {

    @Test
    public void getSecurityManager(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        SecurityManager securityManager = (DefaultSecurityManager)applicationContext.getBean("securityManager");
        System.out.println(securityManager);
    }

    @Test
    public void getSubject(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        Subject subject = SecurityUtils.getSubject();
        System.out.println(subject);
        UsernamePasswordToken token = new UsernamePasswordToken("zhang","123");
        try{
            subject.login(token);
            System.out.println("登录成功");
        }catch (AuthenticationException e){
            //登录失败
            System.out.println("登录失败");
        }

        subject.logout();
    }
}
